<template>
	<view class="">
		<view class="gallery">
			<swiper class="swiper" circular="true" @change="changeImg">
				<block v-for="(item,i) in resData.content" :key="i">
					<swiper-item class="item">
						<image mode="widthFix" class="slide-image" :src="item" />
					</swiper-item>
				</block>
			</swiper>
			<span class="item-num" v-if="resData.num>1">{{activeIndex+1}}/{{resData.num}}</span>
		</view>

		<view class="btn">
			<view class="btn1" @click="hold_img">
				<image src="../../static/images/download.png" class="btn-icon"></image>
				<view class="btn-p">保存到手机</view>
			</view>
			<button open-type="share" class="btn2">
				<image src="../../static/images/share.png" class="btn-icon"></image>
				<view class="btn-p">分享给好友</view>
			</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				activeIndex: 0,
				options: '',
				resData: []
			}
		},

		/**
		 * 生命周期函数--点击事件集合
		 */
		methods: {
			changeImg(e) {
				let that = this
				that.activeIndex = e.detail.current
				// console.log(e)
			},
			hold_img() {
				// 下载图片
				var item = this.resData.content
				for (var i = 0; item.length > i; i++) {
					this.saveimg(item[i],i);
				}
			},
			
			saveimg(imgSrc,i) {
				uni.showLoading({
					title: '保存中...'
				})
				uni.downloadFile({
					url: imgSrc,
					success: res => {
						if(res.statusCode == 200){
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: data => {
									// console.log(data)
									uni.hideLoading()
									uni.showToast({
										title: "保存成功",
										// icon: "none"
									});
								},
								fail: err => {
									// console.log(err)
									if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
									    uni.showModal({
											title: '提示',
											content: '需要您授权保存相册',
											showCancel: false,
											success: modalSuccess => {
												uni.openSetting({
													success(settingdata) {
														if (settingdata.authSetting['scope.writePhotosAlbum']) {
															console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
														} else {
															console.log('获取权限失败，给出不给权限就无法正常使用的提示')
														}
													}
												})
											}
										})
									}
								}
							})
						}
					},
				})
			},
			
			get_info(){
				var post_data = {
					id: this.options.id
				}
				this.$http({
					url: 'Api/Work/iteminfo',
					data: post_data,
					hideLoading: true,
					success: res => {
						this.resData = res.data
						
						uni.setNavigationBarTitle({
						    title: res.data.location
						});
					}
				})
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			this.options = options
			this.get_info()
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady() {

		},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh() {

		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom() {

		},
		onShareAppMessage(e) {
			return {
				title: '展示出最美的地方，来和我一起吧！',
				path: '/pages/index/index' ,//分享的页面路径
				imageUrl: '../../static/images/6_02.jpg'
			}
		}
	}
</script>

<style scoped>
	page {
		background: #000;
	}

	.gallery {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
	}

	.item-num {
		width: 100%;
		height: 130rpx;
		text-align: center;
		line-height: 130rpx;
		font-size: 36rpx;
		color: #fff;
		position: absolute;
		top: 0;
		left: 0;
	}

	.swiper {
		width: 100%;
		height: 100%;
	}

	.item {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.btn {
		width: 100%;
		height: 120rpx;
		display: flex;
		border-top: 1px #4d4d4d solid;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 5;
	}

	.btn1,
	.btn2 {
		flex: 1;
		height: 120rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
		color: #fff;
		flex-direction: column;
		border-radius: 0;
	}

	.btn-icon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 10rpx;
	}

	.btn1 {
		background: #262626;
	}

	.btn2 {
		background: #1a1a1a;
	}
</style>
